package io.agora.openlive.model;

import android.content.Context;

import java.util.Iterator;
import java.util.concurrent.ConcurrentHashMap;

import io.agora.Logger;
import io.agora.rtc.IRtcEngineEventHandler;

public class MyEngineEventHandler {
    public MyEngineEventHandler(Context ctx, EngineConfig config) {
        this.mContext = ctx;
        this.mConfig = config;
    }

    private final EngineConfig mConfig;

    private final Context mContext;

    private final ConcurrentHashMap<AGEventHandler, Integer> mEventHandlerList = new ConcurrentHashMap<>();

    public void addEventHandler(AGEventHandler handler) {
        this.mEventHandlerList.put(handler, 0);
    }

    public void removeEventHandler(AGEventHandler handler) {
        this.mEventHandlerList.remove(handler);
    }

    final IRtcEngineEventHandler mRtcEventHandler = new IRtcEngineEventHandler() {

        @Override
        public void onFirstRemoteVideoDecoded(int uid, int width, int height, int elapsed) {
            Logger.d(this, "[远端第一帧视频解码] uid" + uid + " width" + width + " height" + height + " elapsed" + elapsed);
            Iterator<AGEventHandler> it = mEventHandlerList.keySet().iterator();
            while (it.hasNext()) {
                AGEventHandler handler = it.next();
                handler.onFirstRemoteVideoDecoded(uid, width, height, elapsed);
            }
        }

        @Override
        public void onFirstLocalVideoFrame(int width, int height, int elapsed) {
            Logger.d(this, "[本地第一帧视频] width" + width + " height" + height + " elapsed" + elapsed);
        }

        @Override
        public void onFirstRemoteVideoFrame(int uid, int width, int height, int elapsed) {
            super.onFirstRemoteVideoFrame(uid, width, height, elapsed);
            Logger.d(this, "[远端第一帧视频] uid" + uid + " width" + width + " height" + height + " elapsed" + elapsed);
        }
        @Override
        public void onUserJoined(int uid, int elapsed) {
            Logger.d(this, "[其他用户加入] " + " uid" + uid + " elapsed" + elapsed);
        }

        @Override
        public void onUserOffline(int uid, int reason) {
            // FIXME this callback may return times
            Logger.d(this, "[其他用户离开] " + " uid" + uid + " reason" + reason);
            Iterator<AGEventHandler> it = mEventHandlerList.keySet().iterator();
            while (it.hasNext()) {
                AGEventHandler handler = it.next();
                handler.onUserOffline(uid, reason);
            }
        }

        @Override
        public void onUserMuteVideo(int uid, boolean muted) {
            Logger.d(this, "[其他用户暂停视频] " + " uid" + uid + " 是否暂停" + muted);
        }

        @Override
        public void onUserMuteAudio(int uid, boolean muted) {
            super.onUserMuteAudio(uid, muted);
            Logger.d(this, "[其他用户暂停音频] " + " uid" + uid + " 是否暂停" + muted);
        }

        @Override
        public void onRtcStats(RtcStats stats) {
        }


        @Override
        public void onLeaveChannel(RtcStats stats) {
            Logger.d(this, "[离开成功]");
        }

        @Override
        public void onLastmileQuality(int quality) {
            Logger.d(this, "onLastmileQuality " + quality);
        }

        @Override
        public void onError(int err) {
            super.onError(err);
            Logger.d(this, "[错误] " + err);
        }

        @Override
        public void onJoinChannelSuccess(String channel, int uid, int elapsed) {
            Logger.d(this, "[加入成功] channel" + channel + " uid" + uid + " elapsed" + elapsed);

            Iterator<AGEventHandler> it = mEventHandlerList.keySet().iterator();
            while (it.hasNext()) {
                AGEventHandler handler = it.next();
                handler.onJoinChannelSuccess(channel, uid, elapsed);
            }
        }

        public void onRejoinChannelSuccess(String channel, int uid, int elapsed) {
            Logger.d(this, "[重新加入] channel" + channel + " uid" + uid + " elapsed" + elapsed);
        }

        public void onWarning(int warn) {
            Logger.d(this, "[警告]onWarning " + warn);
        }

        @Override
        public void onApiCallExecuted(String api, int error) {
            super.onApiCallExecuted(api, error);
            Logger.d(this, "[api执行回调] [" + api + "] error" + error);
        }

        @Override
        public void onVideoStopped() {
            super.onVideoStopped();
            Logger.d(this, "[停止视频]");
        }

    };

}
